package com.example.demo.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 时间: 2017/12/15.
 *
 * @author zwf
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @RequestMapping("/test.json")
    @RequiresPermissions("userInfo:add")
    public JSONObject Test(){
        JSONObject object = new JSONObject();
        object.put("Test","HelloWorld");
        object.put("Test","zwf");
        object.put("TEst","zzz");
        
        return object;
    }

    @RequestMapping("/test2.json")
//    @RequiresPermissions("userInfo:delete")
    public JSONObject Test2(){
        JSONObject object = new JSONObject();
        object.put("Test","HelloWorld");
        object.put("Test","zwf");
        object.put("TEst","zzz");
        return object;
    }

    @RequestMapping("/loginOut")
    public void loginOut(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
    }



    // 登录提交地址和applicationontext-shiro.xml配置的loginurl一致。 (配置文件方式的说法)
    @RequestMapping(value="/login")
    public String login(HttpServletRequest request, Map<String, Object> map) throws Exception {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String rememberMe = request.getParameter("rememberMe");
        System.out.println("rememberMe:"+rememberMe);
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            if (!StringUtils.isEmpty(rememberMe)){
                System.out.println("111111111111");
                usernamePasswordToken.setRememberMe(true);
            }
            subject.login(usernamePasswordToken); //登录

        } catch (UnknownAccountException e) {
            System.out.println("账号不存在！");
            e.printStackTrace();
        } catch(IncorrectCredentialsException e){
            System.out.println("密码错误！");
            e.printStackTrace();
        } catch(Exception e){
            e.printStackTrace();
        }


        System.out.println("HomeController.login()");
        // 登录失败从request中获取shiro处理的异常信息。
        // shiroLoginFailure:就是shiro异常类的全类名.
        String exception = (String) request.getAttribute("shiroLoginFailure");

        System.out.println("exception=" + exception);
        String msg = "";
        if (exception != null) {
            if (UnknownAccountException.class.getName().equals(exception)) {
                System.out.println("UnknownAccountException -- > 账号不存在：");
                msg = "UnknownAccountException -- > 账号不存在：";
            } else if (IncorrectCredentialsException.class.getName().equals(exception)) {
                System.out.println("IncorrectCredentialsException -- > 密码不正确：");
                msg = "IncorrectCredentialsException -- > 密码不正确：";
            } else if ("kaptchaValidateFailed".equals(exception)) {
                System.out.println("kaptchaValidateFailed -- > 验证码错误");
                msg = "kaptchaValidateFailed -- > 验证码错误";
            } else {
                msg = "else >> "+exception;
                System.out.println("else -- >" + exception);
            }
        }
        map.put("msg", msg);
        // 此方法不处理登录成功,由shiro进行处理.
        return "/login";
    }


}
